﻿using System;

namespace p_Library
{
    public class cls_ReadNumber
    {
        #region mtd_ChangeNumber

        public static string mtd_ChangeNumber(string text, int di)
        {
            text = text.Replace(",", "");
            string _sign = (text.Length > 0 && text.Substring(0, 1) == "-") ? text.Substring(0, 1) : "";
            text = (text.Length > 0 && text.Substring(0, 1) == "-") ? text.Substring(1) : text;

            if (text.Length > 0)
            {
                //Khai bao phan chan mod va phan le div
                string mod = "";
                string div = "";

                if (text.LastIndexOf(".") > 0)
                {
                    mod = text.Substring(0, text.LastIndexOf("."));
                    decimal x = Math.Round(decimal.Parse("0" + text.Substring(text.LastIndexOf("."))), di);
                    div = x > 0&&di>0 ? x.ToString().Substring(x.ToString().LastIndexOf(".")) : "";
                }
                else
                {
                    mod = text;
                }

                bool temp = true;
                while (temp)
                {
                    if (mod.Length > 1)
                    {
                        if (mod.Substring(0, 1).Equals("0")) mod = mod.Substring(1);
                        else temp = false;
                    }
                    else
                        temp = false;
                }


                //Phan dau cho so
                string result = "";
                while ((mod.Length/3) > 0)
                {
                    result = "," + mod.Substring(mod.Length - 3) + result;
                    mod = mod.Substring(0, mod.Length - 3);
                }
                result = mod
                         + (mod.Length > 0 ? result : result.Substring(1))
                         + (div.Length > 1 ? div : "");


                //Gia tri tra ve
                return _sign + result;
            }
            return "";
        }

        #endregion

        #region Read number

        /// <summary>
        /// Doc so
        /// </summary>
        /// <param name="_digit">so</param>

        #region mtd_ReadDigit

        private static string mtd_ReadDigit(int _digit)
        {
            string _text = "";
            switch (_digit)
            {
                case 0:
                    _text = "không";
                    break;
                case 1:
                    _text = "một";
                    break;
                case 2:
                    _text = "hai";
                    break;
                case 3:
                    _text = "ba";
                    break;
                case 4:
                    _text = "bốn";
                    break;
                case 5:
                    _text = "năm";
                    break;
                case 6:
                    _text = "sáu";
                    break;
                case 7:
                    _text = "bảy";
                    break;
                case 8:
                    _text = "tám";
                    break;
                case 9:
                    _text = "chín";
                    break;
            }
            return _text;
        }

        #endregion

        #region mtd_ReadNumber

        public static string mtd_ReadNumber(int _number)
        {
            string _readnumber = "";
            int _no = 0;

            #region 0-9

            if (_number < 10)
                _readnumber = mtd_ReadDigit(_number);
            #endregion

            #region 10-99

            else if (_number < 100)
            {
                _no = _number / 10;
                switch (_no)
                {
                    case 1:
                        _readnumber = "mười";
                        break;
                    default:
                        _readnumber = mtd_ReadDigit(_no) + " mươi";
                        break;
                }
                _no = _number - _no * 10;
                switch (_no)
                {
                    case 0:
                        _readnumber = _readnumber + "";
                        break;
                    case 1:
                        _readnumber = _readnumber + (_readnumber == "mười" ? " một" : "mốt");
                        break;
                    case 5:
                        _readnumber = _readnumber + " lăm";
                        break;
                    default:
                        _readnumber = _readnumber + " " + mtd_ReadDigit(_no);
                        break;
                }
            }
            #endregion

            #region 100-999

            else if (_number < 1000)
            {
                _no = _number / 100;
                _readnumber = mtd_ReadDigit(_no) + " trăm";

                _no = _number - _no * 100;
                if (_no > 0)
                    _readnumber = _readnumber + (_no < 10 ? " lẻ " : " ") + mtd_ReadNumber(_no);
            }
            #endregion

            #region 1.000-999.999

            else if (_number < 1000000)
            {
                _no = _number / 1000;
                _readnumber = mtd_ReadNumber(_no) + " ngàn";

                _no = _number - _no * 1000;
                if (_no > 0)
                    _readnumber = _readnumber + (_no > 99 ? " " : " không trăm ") + (_no > 9 ? "" : "lẻ ")
                                  + mtd_ReadNumber(_no);
            }
            #endregion

            #region 1.000.000-999.999.999

            else if (_number < 1000000000)
            {
                _no = _number / 1000000;
                _readnumber = mtd_ReadNumber(_no) + " triệu";

                _no = _number - _no * 1000000;
                if (_no > 999)
                    _readnumber = _readnumber + (_no >= 100000 ? " " : " không trăm ") + (_no >= 10000 ? "" : "lẻ ")
                                  + mtd_ReadNumber(_no);
                else if (_no > 0)
                    _readnumber = _readnumber + (_no > 99 ? " " : " không trăm ") + (_no > 9 ? "" : "lẻ ")
                                  + mtd_ReadNumber(_no);
            }

            #endregion

            return _readnumber;
        }

        #endregion

        #endregion
    }
}